<?php
//Lauri Link student 11 12.11.2011
session_start();
echo '<html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            </head>
        <title>Numbrite pakkumine</title>
        <h2 align=center>On mingi väärtus vahemikus 1 ... 1000.<br /> Ürita ära arvata.
            </h2>';
$guess = $_POST['pakkumine'];
$value = $_POST['suva'];
$counter = $_POST['lugeja'];
if ($_POST['uus'] == "Proovin")
    {
    if  (is_numeric ($guess))
        {
        if ($guess == $value)
            {
            $result = "No seekord läks hästi.<br><br><b>Õige vastus on $value.</b><br>";
            $counter ++;
            $_SESSION[$answers][$counter] = $guess;
            }
        elseif ($guess > $value)
            {
            $result = "Liiga palju.<br>";
            $counter ++;
            $_SESSION[$answers][$counter] = $guess;
            }
        else
            {
            $result = "Liiga vähe.<br>";
            $counter ++;
            $_SESSION[$answers][$counter] = $guess;
            }
        }
    else
        {
        $result = "Üritaks siiski numbritega.<br>";
        $counter ++;
        $_SESSION[$answers][$counter] = $guess;
        }
    }
elseif ($_POST['uus'] == "Alustan uuesti")
    {
    $value = rand (1, 1000);
    $result = "<br>";
    $counter = 0;
    $guess = "";
    $_SESSION = array();
    }

?>
<html>
<head>
<title>Arvamine</title>
</head>
<body bgcolor=AliceBlue text=DarkGoldenRod link=DarkSlateGray></body>
<p>
<table border=0 align=center>
    <tr>
        <td>  Sinu pakkumine :</td><td></td>
    </tr>
    <tr>
        <td><form method=post action=<?php echo $PHP_SELF;?>>
        <input type=text name=pakkumine maxlength=4 value=<?php echo $guess; ?>></td><td></td>
    </tr>
    <tr>
        <td><input type=submit name=uus value="Proovin">
        <input type=submit name=uus value="Alustan uuesti"></td>
    </tr>
    <tr>
        <td><input type=hidden name=suva value=<?php echo $value; ?>>
        <input type=hidden name=lugeja value=<?php echo $counter; ?>></form>
        </td>
    </tr>
    <tr>
        <td>
<?php

echo $result;
$k = "korda";
if ($counter == 1)
    {
    $k = "kord";
    }

echo "Loendur : <b> $counter</b> $k <br>
        </td>
    </tr></table><pre><table align=center><tr>";
if ($_SESSION[$answers])
    {
    sort ($_SESSION[$answers]);
    foreach($_SESSION[$answers] as $key => $val)
        {
        $color = "Green";
        if ($val < $value)
            {
            $color = "Blue";
            }
        elseif ($val > $value)
            {
            $color = "Red"; 
            }
        echo "<td><font color=$color align=center > <b> $val </b></td>";
        }
    }
?></tr></table>
</html>